package chapter06

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test06_ConstructorParams
 * Package: chapter06
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-06-22
 *          Time: 18:34
 */

object Test06_ConstructorParams {
  def main(args: Array[String]): Unit = {
    val student2 = new Student2
    student2.name = "alice"
    student2.age = 20
    println(s"student2: name = ${student2.name} , age = ${student2.age}")

    //这样定义就要传参数了
    val student3 = new Student3("bob", 18)
    println(s"student3: name = ${student3.name} , age = ${student3.age}")

    //没有修饰定义的就是一个局部变量 一个参数而已
    val student4 = new Student4("pop",99)
    //println(s"student3: name = ${student4.name} , age = ${student4.age}")
    //在类中单独写一个方法 然后调用
    student4.printInfo

    val student5 = new Student5("不能在更改", 100)
    //student5.name = "hahah" 直接就报错了
    println(s"student5: name = ${student5.name} , age = ${student5.age}")




  }
}


//定义类
//无参构造器
class Student2 {
  //没有定义参数 就在里面写相关属性
  var name: String = _
  var age: Int = _
  //如果这是单独得这样定义 下面一定要否辅助构造器进行传值的
}

//等价的定义方式 添加参数列表
//并且又是构造方法 又
class Student3(var name: String, var age: Int)

//主构造器参数无修饰
class Student4(name: String, age: Int){
  def printInfo={
    println(s"student4: name = ${this.name} , age = ${this.age}")
  }
}

//使用val修饰 不能更改这个值 相当于是个常量
class Student5(val name: String, val age: Int)

//主构造器 和 辅助构造器同时存在
class Student6(var name: String, var age: Int){
  //就没必要在 辅助构造器声明主构造器有的方法了
  //另外定义一个变量
  var school:String = _
  def this(name: String, age: Int,school: String){
    this(name,age) //调用主构造器
    this.school = school
  }

}



